﻿using BCCommon;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplate;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateBusiness;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateBusiness
{
    public class WorkflowTemplateBusinessData : IWorkflowTemplateBusinessData
    {
        private readonly IDatabaseContext databaseContext;

        public WorkflowTemplateBusinessData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<IEnumerable<WorkflowTemplateBusinessEntity>> GetAllowInitiateList(int companyId, int departmentId, int positionId, long adminId,string categoryCode)
        {
            string sql = @"SELECT A.WorkflowTemplateId,A.TemplateCode,A.TemplateName,A.CategoryCode,A.CategoryName,A.IsBaseFlow,A.IsMobleShow,A.AllowInitiate,A.IsAllPerson,
                                B.PermissionReferenceId,B.PermissionReferenceName,B.PermissionTypeStr,B.PermissionType
                            FROM oa_workflowtemplate A
                            INNER JOIN OA_WorkflowTemplatePermission B ON B.TemplateCode=A.TemplateCode";
            List<string> orWheres = new List<string>();
            List<string> andWheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            andWheres.Add("A.Enabled=1");

            orWheres.Add("(B.PermissionType='Company' AND B.PermissionReferenceId=?CompanyId)");
            parameters.Add("?CompanyId", companyId, DbType.Int32);
            orWheres.Add("(B.PermissionType='Department' AND B.PermissionReferenceId=?DepartmentId)");
            parameters.Add("?DepartmentId", departmentId, DbType.Int32);
            orWheres.Add("(B.PermissionType='Position' AND B.PermissionReferenceId=?PositionId)");
            parameters.Add("?PositionId", positionId, DbType.Int32);
            orWheres.Add("(B.PermissionType='Human' AND B.PermissionReferenceId=?AdminId)");
            parameters.Add("?AdminId", adminId, DbType.Int64);

            if (!string.IsNullOrWhiteSpace(categoryCode))
            {
                andWheres.Add("A.CategoryCode=?CategoryCode");
                parameters.Add("?CategoryCode", categoryCode.Trim(), DbType.AnsiString, size: 50);
            }

            string querySql = string.Format("{0} {1}", sql, orWheres.Count > 0 ? " WHERE " + string.Join(" OR ", orWheres) : "");
            if(andWheres.Count > 0&& orWheres.Count > 0)
            {
                querySql = string.Format("{0} {1}", " AND " + string.Join(" AND ", andWheres));
            }
            else if (andWheres.Count>0 && orWheres.Count == 0)
            {
                querySql = string.Format("{0} {1}", " WHERE " + string.Join(" AND ", andWheres));
            }
           
            return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateBusinessEntity>(querySql, parameters);
        }
    }
}
